package com.tsxy.mall.service.impl;

import com.tsxy.mall.entity.User;
import com.tsxy.mall.mapper.UserMapper;
import com.tsxy.mall.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

@Service
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;
    @Override
    public User login(User user) throws Exception {
        User selectUser = userMapper.getByUserName(user.getUsername());
        if (selectUser == null){
            throw new Exception("没有该用户");
        }
        if ( ! selectUser.getPassword().equals(user.getPassword())){
            throw new Exception("密码错误");
        }
        return selectUser;
    }

    @Override
    public int reg(User user, String rePassword) {
        // 1- 验证输入格式
        if(user.getUsername().length() < 3 || user.getUsername().length() > 16){
            return -1;
        }
        if(user.getPassword().length() < 3 || user.getPassword().length() > 16){
            return -2;
        }
        // 2- 比较两次密码是否一致
        if(user.getPassword().length() == 0  || rePassword.length() == 0){
            return -5;
        }
        if (!rePassword.equals(user.getPassword())){
            return -3;
        }
        // 3- 判断账号是否可用
        User selectUser = userMapper.getByUserName(user.getUsername());
        if (selectUser != null){
            return -4;
        }
        return userMapper.reg(user);
    }
}
